<!--
 * @Author: zhaoziyin
 * @Date: 2022-10-12 15:09:04
 * @LastEditTime: 2022-10-16 17:05:32
 * @LastEditors: zhaoziyin
 * @Description: 一直游到海水变蓝
 * @FilePath: \项目d:\桌面\新建文件夹 (2)\home-economics-cloud-service\src\pages\admin\waiter\List.vue
 * 别乱动！
-->
<template>
  <!-- 员工管理页 -->
  <div>
    <!-- 第一行搜索框 开始 -->
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-button type="primary" @click="toAdd">添加</el-button>
      <el-form-item>
        <el-input
          clearable
          v-model="params.username"
          placeholder="请输入用户"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select clearable v-model="params.status" placeholder="请选择状态">
          <el-option label="禁用" value="禁用"></el-option>
          <el-option label="正常" value="正常"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 第一行搜索框 结束 -->
    <!-- 第二行 表格开始 -->
    <el-table ref="myForm" :data="customeData" stripe style="width: 100%">
      <el-table-column prop="id" label="日期" width="180"> </el-table-column>
      <el-table-column prop="username" label="用户名" width="180">
      </el-table-column>
      <el-table-column prop="realname" label="姓名"> </el-table-column>
      <el-table-column prop="talephone" label="手机号"> </el-table-column>
      <el-table-column prop="gender" label="性别">
        <!-- 作用域插槽 根据条件判断显示 -->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender === '男'" type="info">男</el-tag>
          <el-tag v-else>女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '正常'" type="success"
            >正常</el-tag
          >
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <a href="#" @click.prevent="toEdit(scope.row)" class="toEdit">编辑</a>
          <a href="#" @click.prevent="toDel(scope.row.id)" class="toDel">删除</a>
          <a href="#" @click.prevent="toDetails(scope.row.id)" class="toDetails"
            >详情</a
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 第二行 表格结束 -->
    <!-- 第三行分页开始 -->
    <el-pagination
      class="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.page"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="params.pageSize"
      layout="total,sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 第三行分页结束 -->
    <!-- 第四行 模态框开始 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form ref="myForm" :rules="rules" :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.realname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.telephone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio v-model="form.gender" label="男">男</el-radio>
          <el-radio v-model="form.gender" label="女">女</el-radio>
        </el-form-item>
        <el-form-item v-if="form.status" label="状态" :label-width="formLabelWidth">
          <el-radio v-model="form.status" label="正常">正常</el-radio>
          <el-radio v-model="form.status" label="禁用">禁用</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定
        </el-button>
      </div>
    </el-dialog>
    <!-- 第四行 模态框结束 -->
  </div>
</template>

<script>
import { pageQuery, addUserWidthRole, saveOrUpdate, deleteById } from "@/api/user";
export default {
  data() {
    return {
      // 表单对象和分页数据存放的对象
      params: {
        page: 1,
        pageSize: 10,
        roleId: 1,
      },
      formLabelWidth: "120px",
      total: 0,
      // 顾客数据
      customeData: [],
      // 模态框的显示与关闭
      visible: false,
      // 标题名称
      title: "",
      // 表单对象
      form: {},
      // 校验规则
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: blur }],
        password: [{ required: true, message: "请输入密码", trigger: blur }],
      },
    };
  },
  computed: {},
  methods: {
    // 点击跳转详情页面
    async toDetails(id){
      this.$router.push({
        path:'/admin/waiter/Details',
        query:{id}
      })
    },
    // 点击添加按钮
    toAdd() {
      this.visible = true;
      this.title = "添加员工信息";
      this.form = {};
    },
    // 点击编辑按钮
    toEdit(row) {
      this.visible = true;
      this.title = "编辑员工信息";
      this.form = { ...row };
    },
    // 点击删除按钮
    toDel(id) {
      this.$confirm("此操作将永久修改数据，是否继续？", "提示", {
        comfirmButtonText: "确定",
        comfirmButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteById({ id });
          this.$message({
            type: "success",
            message: res.message,
          });
          // 局部刷新
          this.getCustome();
        })
        .catch(() => {
          this.$message({
            type: "success",
            message: "已取消删除",
          });
        });
    },
    async toSubmit() {
      await onSubmit();
    },
    // 点击确认按钮执行保存更新
    onSubmit() {
      // 判断表单中是否存在id，有的话编辑，没有的话时添加
      if (this.form.id) {
        // 校验表单
        this.$refs.myForm.validate(async (valid) => {
          if (valid) {
            let res = await saveOrUpdate(this.form);
            // console.log(res);
            this.customeData = res.data;
            this.$message({
              type: "success",
              message: res.message,
            });
            this.visible = false;
            this.getCustome();
            // 点击确定重置表单
          } else {
            this.$message({
              type: error,
              message: "请输入",
            });
          }
        });
      }else{
        this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          console.log(this.form);
          let res = await addUserWidthRole({...this.form,roleId:1});
          this.$message({
            type: "success",
            message: res.message,
          });
          this.visible = false;
          this.getCustome();
          // 点击确定重置表单
        }else{
          this.$message({
            type:error,
            message:"请输入"
          })
        }
      });
      }
    },
    // 点击查询按钮
    onSearch() {
      this.params.pageSize = 5;
      this.getCustome();
    },
    // 分页查询顾客数据
    async getCustome() {
      for (let key in this.params) {
        if (!this.params[key]) {
          delete this.params[key];
        }
      }
      let res = await pageQuery(this.params);
      this.customeData = res.data.list;
      this.total = res.data.total;
    },
    // 页码条数发送改变的方法 默认参数pageSize
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.page = 1;
      this.getCustome();
    },
    // 当前页码发送改变触发的方法 默认参数page
    handleCurrentChange(page) {
      this.params.page = page;
      this.getCustome();
    },
  },
  created() {
    this.getCustome();
  },
  mounted() {},
};
</script>
<style scoped>
.page{
  text-align: right;
  margin-top: 20px;
}

.dialog-footer{
  border-top: 1px solid #30b08f;
  padding: 20px;
}
.toEdit {
  color: #30b08f;
}
.toDel {
  color: red;
}
.details {
  color: rgb(58, 113, 168);
}
</style>